<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>生成器</title>
    <script src="js/traceur.js"></script>
    <script src="js/bootstrap.js"></script>
    <script type="module">
        //生成器
        function* generator_function() {
            yield 1;
            yield 2;
            yield 3;
            yield 4;
            yield 5;
        }
        let generator = generator_function();
        console.log(generator.next().value); // 1
        console.log(generator.next().value); // 2
        console.log(generator.next().value); // 3
        console.log(generator.next().value); // 4
        console.log(generator.next().value); // 5
        console.log(generator.next().done);  // true
        generator = generator_function();  //重新赋值生成器
        //@@iterator方法必须返回一个迭代器对象。
        let iterable = generator[Symbol.iterator]();
        console.log(iterable.next().value); // 1
        console.log(iterable.next().value); // 2
        console.log(iterable.next().value); // 3
        console.log(iterable.next().value); // 4
        console.log(iterable.next().value); // 5
        console.log(iterable.next().done); // true

        //yield 关键字后面的表达式
        function* generator_function1() {
            var a = yield 12;
            var b = yield a + 1;
            var c = yield b + 2;
            yield c + 3;
        }
        var generator1 = generator_function1();
        console.log(generator1.next().value); // 12
        console.log(generator1.next(5).value); // 6
        console.log(generator1.next(11).value); // 13
        console.log(generator1.next(78).value); // 81
        console.log(generator1.next().done); // true

        //使用return方法提交结束生成器
        function* generator_function2() {
            yield 1;
            yield 2;
            yield 3;
        }
        var generator2 = generator_function2();
        console.log(generator2.next().value); // 1
        console.log(generator2.return(22).value); // 22
        console.log(generator2.next().done); // true

        //处理异常
        function* generator_function3() {
            try {
                yield 1;
            }
            catch (e) {
                console.log(e); //异常字符串类型
            }
            try {
                yield 2;
            }
            catch (e) {
                console.log("2nd Exception");
            }
        }
        var generator3 = generator_function3();
        console.log(generator3.next().value); // 1
        console.log(generator3.throw("exception string").value); //exception string,继续执行下一个异常
        console.log(generator3.throw("exception string").done);

        //yiedl* 关键字后面带迭代对象
        function* generator_function_1() {
            yield 2;
            yield 3;
        }
        function* generator_function_2() {
            yield 1;
            yield* generator_function_1();
            yield* [4, 5];
        }
        var generator4 = generator_function_2();
        console.log(generator4.next().value); // 1
        console.log(generator4.next().value); // 2
        console.log(generator4.next().value); // 3
        console.log(generator4.next().value); // 4
        console.log(generator4.next().value); // 5
        console.log(generator4.next().done); // true

    </script>
</head>
<body>

</body>
</html>